package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.JsonUtils;
import com.heima.model.media.dtos.WmNewsContentItem;
import com.heima.model.media.dtos.WmNewsDto;
import com.heima.wemedia.entity.WmMaterial;
import com.heima.wemedia.entity.WmNewsMaterial;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.service.WmNewsMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * 自媒体图文引用素材信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Service
public class WmNewsMaterialServiceImpl extends ServiceImpl<WmNewsMaterialMapper, WmNewsMaterial> implements WmNewsMaterialService {

    @Autowired
    private WmMaterialService materialService;

    @Override
    public void deleteByWmNewsId(Integer wmNewsId) {
        LambdaQueryWrapper<WmNewsMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNewsMaterial::getNewsId, wmNewsId);
        this.remove(queryWrapper);
    }

    @Override
    public void insertByWmNews(WmNewsDto dto) {
        Collection<WmNewsMaterial> newsMaterialList = new ArrayList<>();
        // 封面图
        List<String> images = dto.getImages();
        if(!CollectionUtils.isEmpty(images)){
            for(String url : images){
                WmNewsMaterial wmNewsMaterial = new WmNewsMaterial();
                WmMaterial wmMaterial = materialService.getByUrl(url);
                if(wmMaterial == null){
                    throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
                }
                wmNewsMaterial.setMaterialId(wmMaterial.getId());
                wmNewsMaterial.setNewsId(dto.getId());
                wmNewsMaterial.setOrd(1);
                wmNewsMaterial.setType(1);
                newsMaterialList.add(wmNewsMaterial);
            }
        }
        // 正文图
        String content = dto.getContent();
        List<WmNewsContentItem> wmNewsContentItems = JsonUtils.toList(content, WmNewsContentItem.class);
        for(WmNewsContentItem item : wmNewsContentItems){
            String type = item.getType();
            if(Objects.equals(type, WmNewsContentItem.IMAGE_TYPE)){
                WmNewsMaterial wmNewsMaterial = new WmNewsMaterial();
                wmNewsMaterial.setMaterialId(item.getId());
                wmNewsMaterial.setNewsId(dto.getId());
                wmNewsMaterial.setOrd(1);
                wmNewsMaterial.setType(0);
                newsMaterialList.add(wmNewsMaterial);
            }
        }
        if(!CollectionUtils.isEmpty(newsMaterialList)){
            this.saveBatch(newsMaterialList);
        }
    }
}
